#!/bin/bash
set -e

svn_master='https://rathena.svn.sourceforge.net/svnroot/rathena/'
svn_slave1='file:///var/www/svn/rathena/'
svn_slave2='https://rathena.googlecode.com/svn/'

TMPFILE=`mktemp -t svn_head.XXXXXX`
svn log -q -r "HEAD" "$svn_master" > $TMPFILE
rev_master=`cat $TMPFILE | sed -ne '/^r[0-9]/{ s/^r//; s/ .*//; p; q; }'`
svn log -q -r "HEAD" "$svn_slave1" > $TMPFILE
rev_slave1=`cat $TMPFILE | sed -ne '/^r[0-9]/{ s/^r//; s/ .*//; p; q; }'`
svn log -q -r "HEAD" "$svn_slave2" > $TMPFILE
rev_slave2=`cat $TMPFILE | sed -ne '/^r[0-9]/{ s/^r//; s/ .*//; p; q; }'`
rm -f $TMPFILE

if [ "$rev_slave1" -lt "$rev_master" ]
then
	echo "Updating mirror from $rev_slave1 to $rev_master ..."
	svnsync sync --sync-username rathenabot $svn_slave1
elif [ "$rev_slave1" = "$rev_master" ]
then
	echo "Mirror is up to date at revision $rev_slave1"
fi

if [ "$rev_slave2" -lt "$rev_master" ]
then
	echo "Updating mirror from $rev_slave2 to $rev_master ..."
	svnsync sync --sync-username rathenabot $svn_slave2
elif [ "$rev_slave2" = "$rev_master" ]
then
	echo "Mirror is up to date at revision $rev_slave2"
fi
